package com.upload.pic;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.xike.kyt.R;
import com.upload.pic.ImageGridAdapter.TextCallback;

public class UploadSelectPicAct extends Activity {
	public static final String EXTRA_IMAGE_LIST = "imagelist";

	// ArrayList<Entity> dataList;//用来装载数据源的列表
	List<ImageItem> dataList;
	GridView gridView;
	ImageGridAdapter adapter;// 自定义的适配器
	AlbumHelper helper;
	Button bt;

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				Toast.makeText(UploadSelectPicAct.this, "最多选择9张图片", 400).show();
				break;

			default:
				break;
			}
		}
	};

	private int tag;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.act_upload_select_pic);
		tag = getIntent().getExtras().getInt("tag");

		helper = AlbumHelper.getHelper();
		helper.init(getApplicationContext());

		dataList = (List<ImageItem>) getIntent().getSerializableExtra(EXTRA_IMAGE_LIST);

		findViewById(R.id.layoutTitle_tv_cancel).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (UploadSelectPhotoAct.instance != null) {
					UploadSelectPhotoAct.instance.finish();
				}
				finish();
			}
		});

		findViewById(R.id.layoutTitle_btn_back).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		bt = (Button) findViewById(R.id.bt);
		bt.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				ArrayList<String> list = new ArrayList<String>();
				Collection<String> c = adapter.map.values();
				Iterator<String> it = c.iterator();
				for (; it.hasNext();) {
					list.add(it.next());
				}

				if (Bimp.act_bool) {
					// Intent intent = new Intent(ImageGridActivity.this,
					// PublishedActivity.class);
					// startActivity(intent);
					// Bimp.act_bool = false;
				}
				for (int i = 0; i < list.size(); i++) {
					if (tag == 1) {
						if (Bimp.drr2.size() < 9) {
							Bimp.drr2.add(list.get(i));
						}
					} else {
						if (Bimp.drr.size() < 9) {
							Bimp.drr.add(list.get(i));
						}
					}

				}
				if (UploadSelectPhotoAct.instance != null) {
					UploadSelectPhotoAct.instance.finish();
				}
				finish();
			}
		});
		initView();
	}

	/**
	 * 初始化view视图
	 */
	private void initView() {
		gridView = (GridView) findViewById(R.id.gridview);
		gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		adapter = new ImageGridAdapter(UploadSelectPicAct.this, dataList, mHandler,tag);
		gridView.setAdapter(adapter);
		if (tag == 1) {
			bt.setText("完成" + "(" + Bimp.max2 + "/9)");
			adapter.setTextCallback(new TextCallback() {
				public void onListen(int count) {
					bt.setText("完成" + "(" + (count + Bimp.max2) + "/9)");
				}
			});
		} else {
			bt.setText("完成" + "(" + Bimp.max + "/9)");
			adapter.setTextCallback(new TextCallback() {
				public void onListen(int count) {
					bt.setText("完成" + "(" + (count + Bimp.max) + "/9)");
				}
			});
		}

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				/**
				 * 根据position参数，可以获得跟GridView的子View相绑定的实体类，然后根据它的isSelected状态，
				 * 来判断是否显示选中效果。 至于选中效果的规则，下面适配器的代码中会有说明
				 */
				// if (dataList.get(position).isSelected) {
				// dataList.remove(position);
				// } else {
				// dataList.get(position).setSelected(true);
				// }
				/**
				 * 通知适配器，绑定的数据发生了改变，应当刷新视图
				 */
				adapter.notifyDataSetChanged();
			}

		});

	}
	
	
}
